package com.drug.web.controller.wms;

import com.drug.common.annotation.Log;
import com.drug.common.core.controller.BaseController;
import com.drug.common.core.domain.AjaxResult;
import com.drug.common.core.page.TableDataInfo;
import com.drug.common.enums.BusinessType;
import com.drug.common.utils.SecurityUtils;
import com.drug.common.utils.poi.ExcelUtil;
import com.drug.wms.domain.BusGoodsStockDetail;
import com.drug.wms.domain.BusGoodsStockDetailList;
import com.drug.wms.domain.BusGoodsStockInfo;
import com.drug.wms.domain.BusGoodsStockInfoRight;
import com.drug.wms.service.IBusGoodsStockDetailService;
import com.drug.wms.service.IBusGoodsStockInfoService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品入库清单Controller
 *
 * @author ruoyi
 * @date 2021-12-01
 */
@Api("入库清单管理")
@RestController
@RequestMapping("/wms/goodsStock")
public class BusGoodsStockInfoController extends BaseController {
    @Autowired
    private IBusGoodsStockInfoService busGoodsStockInfoService;
    @Autowired
    private IBusGoodsStockDetailService busGoodsStockDetailService;

    /**
     * 查询商品入库清单列表
     */
    @ApiOperation("查询商品入库清单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "docNo", value = "单据编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "docName", value = "单据名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "entryDate", value = "录入日期", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "entryUserId", value = "制单人id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "entryDeptId", value = "制单人部门id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "supplyCompany", value = "供货单位", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "receivingWarehouse", value = "收货仓库", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "handleUserId", value = "经办人", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkedUserId", value = "审核人", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "summary", value = "摘要", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "inputInvoiceNo", value = "进项发票号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "paymentAmount", value = "付款金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "isPay", value = "是否付款", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "inputStatus", value = "单据状态", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "validityCode", value = "数据状态", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("/list")
    public TableDataInfo list(BusGoodsStockInfo busGoodsStockInfo) {
        startPage();
        PageHelper.orderBy("create_time desc");
        return getDataTable(busGoodsStockInfoService.selectBusGoodsStockInfoList(busGoodsStockInfo));
    }

    /**
     * 查询商品入库清单列表详细信息
     */
    @ApiOperation("查询商品入库清单列表详细信息")
    @ApiImplicitParam(name = "stockId", value = "入库清单主键id", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping("/detail/list/{stockId}")
    public TableDataInfo detailList(@PathVariable("stockId") String stockId) {
        startPage();
        PageHelper.orderBy("create_time desc");
        BusGoodsStockDetail busGoodsStockDetail = new BusGoodsStockDetail();
        busGoodsStockDetail.setStockId(stockId);
        List<BusGoodsStockDetail> list = busGoodsStockDetailService.selectBusGoodsStockDetailList(busGoodsStockDetail);
        //手动处理
        return getDataTable(list);
    }

    /**
     * 导出商品入库清单列表
     */
    @ApiOperation("导出商品入库清单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "docNo", value = "单据编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "docName", value = "单据名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "entryDate", value = "录入日期", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "entryUserId", value = "制单人id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "entryDeptId", value = "制单人部门id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "supplyCompany", value = "供货单位", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "receivingWarehouse", value = "收货仓库", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "handleUserId", value = "经办人", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkedUserId", value = "审核人", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "summary", value = "摘要", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "inputInvoiceNo", value = "进项发票号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "paymentAmount", value = "付款金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "isPay", value = "是否付款", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "inputStatus", value = "单据状态", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "validityCode", value = "数据状态", dataType = "String", dataTypeClass = String.class)
    })
    @Log(title = "商品入库清单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusGoodsStockInfo busGoodsStockInfo) {
        List<BusGoodsStockInfo> list = busGoodsStockInfoService.selectBusGoodsStockInfoList(busGoodsStockInfo);
        ExcelUtil<BusGoodsStockInfo> util = new ExcelUtil<>(BusGoodsStockInfo.class);
        return util.exportExcel(list, "商品入库清单数据");
    }

    /**
     * 获取商品入库清单详细信息
     */
    @ApiOperation("获取商品入库清单详细信息")
    @ApiImplicitParam(name = "id", value = "入库清单主键id", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(busGoodsStockInfoService.selectBusGoodsStockInfoById(id));
    }



    /**
     * 新生成入库单据
     */
    @ApiOperation("新生成入库单据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "docName", value = "单据名称", required = true,dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "生成商品入库清单", businessType = BusinessType.INSERT)
    @PostMapping("newOrder")
    public AjaxResult newOrder(@RequestBody BusGoodsStockInfo busGoodsStockInfo) {
        return AjaxResult.success(busGoodsStockInfoService.insertBusGoodsStockInfo2(busGoodsStockInfo));
    }

    /**
     * 修改入库单据
     */
    @ApiOperation("修改入库单据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "docNo", value = "单据编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "docName", value = "单据名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "supplyCompany", value = "供货单位", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "receivingWarehouse", value = "收货仓库", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "summary", value = "摘要", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "inputInvoiceNo", value = "进项发票号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "paymentAmount", value = "付款金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "isPay", value = "是否付款", dataType = "Long", dataTypeClass = Long.class),
    })
    @Log(title = "修改商品入库清单", businessType = BusinessType.UPDATE)
    @PostMapping("order/edit")
    public AjaxResult edit(@RequestBody BusGoodsStockInfo busGoodsStockInfo) {
        return AjaxResult.success(busGoodsStockInfoService.updateBusGoodsStockInfo(busGoodsStockInfo));
    }

    /**
     * 新增商品入库清单详情
     */
    @ApiOperation("新增商品入库清单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stockId", value = "单据主键编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "goodsNo", value = "商品编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "goodsName", value = "商品名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "barcode", value = "商品条码", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "batchNo", value = "批号", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "norms", value = "规格", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "basicUnitNo", value = "基本单位编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "receivingNo", value = "收货仓库编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "receivingGoodsNo", value = "收货货位编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "approvalNumber", value = "批准文号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "productionDate", value = "生产日期", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "expireDate", value = "有效期至", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "reportNumber", value = "检验报告号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "unitNo", value = "单位", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "goodsNum", value = "数量", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "goodsPrice", value = "单价", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "totalPrice", value = "金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "commodityTax", value = "商品税率", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "unitPriceIncludingTax", value = "含税单价", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "totalPriceIncludingTax", value = "含税金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "totalTax", value = "税额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "retailPrice", value = "零售价", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "unitPriceIncludingTax", value = "零售金额", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
            @ApiImplicitParam(name = "qualityStatus", value = "质量状况", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "gift", value = "是否赠品", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "validityCode", value = "数据状态", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createUser", value = "创建人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "updateUser", value = "更新用户ID", dataType = "Long", dataTypeClass = Long.class)

    })
    @Log(title = "新增商品入库清单详情", businessType = BusinessType.INSERT)
    @PostMapping("addDetail")
    public AjaxResult addDetail(BusGoodsStockDetail busGoodsStockDetail) {
        return toAjax(busGoodsStockDetailService.insertBusGoodsStockDetail(busGoodsStockDetail));
    }

    /**
     * 新增商品
     */
    @ApiOperation("批量新增商品入库清单详情")
    @Log(title = "批量新增商品入库清单详情", businessType = BusinessType.INSERT)
    @PostMapping("addDetailList")
    public AjaxResult addDetail(@RequestBody BusGoodsStockDetailList busGoodsStockDetail) {
        return toAjax(busGoodsStockDetailService.insertBusGoodsStockDetail(busGoodsStockDetail));
    }

    /**
     * 新增商品入库清单(提交)
     */
    @ApiOperation(" 新增商品入库清单(提交)")
    @ApiImplicitParam(name = "id", value = "入库清单主键id", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @Log(title = "提交商品入库清单", businessType = BusinessType.INSERT)
    @GetMapping("submit/{id}")
    public AjaxResult submit(@PathVariable("id")String id) {
        BusGoodsStockInfo reqPo = new BusGoodsStockInfo();
        reqPo.setId(id);
        List<BusGoodsStockInfo> busGoodsStockInfos = busGoodsStockInfoService.selectBusGoodsStockInfoList(reqPo);
        if(busGoodsStockInfos.size()==1){
            BusGoodsStockInfo stockInfo = new BusGoodsStockInfo();
            stockInfo.setId(busGoodsStockInfos.get(0).getId());
            stockInfo.setCheckedUserId(SecurityUtils.getUserId());
            stockInfo.setInputStatus(1L);
            return toAjax(busGoodsStockInfoService.updateBusGoodsStockInfo(stockInfo));
        }else {
            return AjaxResult.error("获取商品清单失败");
        }

    }
    /**
     * 获取最近商品入库清单详细信息
     */
    @ApiOperation(" 获取最近商品入库清单详细信息")
    @ApiImplicitParam(name = "goodsNo", value = "商品编号", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping(value = "/latest/{goodsNo}")
    public AjaxResult getGoodsLatest(@PathVariable("goodsNo") String goodsNo) {
        return AjaxResult.success(busGoodsStockDetailService.selectBusGoodsStockDetailLatestByGoodsNo(goodsNo));
    }

    /**
     * 修改商品
     */
    @Log(title = "修改商品入库清单详情", businessType = BusinessType.UPDATE)
    @PostMapping("editDetail")
    public AjaxResult editDetail(@RequestBody BusGoodsStockDetail[] busGoodsStockDetail) {
        return toAjax(busGoodsStockDetailService.updateBusGoodsStockDetail(busGoodsStockDetail));
    }

    /**
     * 删除商品入库清单详情
     */
    @ApiOperation("删除商品入库清单详情")
    @ApiImplicitParam(name = "ids", value = "入库清单详情主键ids", required = true,allowMultiple = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @Log(title = "删除商品入库清单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/detail/{ids}")
    public AjaxResult removeDetail(@PathVariable String[] ids) {
        return toAjax(busGoodsStockDetailService.deleteBusGoodsStockDetailByIds(ids));
    }

    /**
     * 删除商品入库清单
     */
    @ApiOperation("删除商品入库清单")
    @ApiImplicitParam(name = "ids", value = "入库清单主键ids", required = true,allowMultiple = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @Log(title = "商品入库清单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(busGoodsStockInfoService.deleteBusGoodsStockInfoByIds(ids));
    }
}
